package com.ecole.mines.emargement;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.ecole.mines.model.Eleve;

public class SignatureView extends View {

	FragmentListeEleves fragListeEleves;
	Eleve eleveSelectionne;	
	boolean signatureActive = false;
	boolean zoneSignatureVide = false;
	//drawing path
	public Path drawPath;
	//drawing and canvas paint
	public Paint drawPaint, canvasPaint;
	//initial color
	private int paintColor = 0xFF660000;
	//canvas
	public Canvas drawCanvas;
	//canvas bitmap
	private Bitmap canvasBitmap;
	
	public SignatureView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		setupDrawing();
	}
	
	
	/*
	 * Getters / Setters
	 */
	
	public FragmentListeEleves getFragListeEleves() {
		return fragListeEleves;
	}



	public void setFragListeEleves(FragmentListeEleves fragListeEleves) {
		this.fragListeEleves = fragListeEleves;
	}



	public void setSignatureActive(boolean signatureActive) {
		if (signatureActive)
		{
			FragmentListeEleves.activerBoutons(fragListeEleves.getValider(),fragListeEleves.getAnnuler());
		}
		else
		{
			FragmentListeEleves.desactiverBoutons(fragListeEleves.getValider(),fragListeEleves.getAnnuler());
		}
		this.signatureActive = signatureActive;
	}



	public boolean isZoneSignatureVide() {
		return zoneSignatureVide;
	}

	public boolean getZoneSignatureVide() {
		return zoneSignatureVide;
	}

	public void setZoneSignatureVide(boolean zoneSignatureVide) {
		this.zoneSignatureVide = zoneSignatureVide;
	}


	public void setDrawCanvas(Canvas drawCanvas) {
		this.drawCanvas = drawCanvas;
	}

	
	/*
	 * Constructeurs
	 */
	public SignatureView(Context context, AttributeSet attrs) {
		super(context, attrs);
		setupDrawing();
	}

	public SignatureView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
		setupDrawing();
	}
	
	public void setupDrawing(){
		drawPath = new Path();
		drawPaint = new Paint();
		drawPaint.setColor(paintColor);
		drawPaint.setAntiAlias(true);
		drawPaint.setStrokeWidth(5);
		drawPaint.setStyle(Paint.Style.STROKE);
		drawPaint.setStrokeJoin(Paint.Join.ROUND);
		drawPaint.setStrokeCap(Paint.Cap.ROUND);
		
		canvasPaint = new Paint(Paint.DITHER_FLAG);
		
		setEnabled(true);
		}

	
	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		super.onSizeChanged(w, h, oldw, oldh);
		canvasBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
		drawCanvas = new Canvas(canvasBitmap);
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		canvas.drawColor(Color.parseColor("#CEDBDE"));
		canvas.drawBitmap(canvasBitmap, 0, 0, canvasPaint);
		canvas.drawPath(drawPath, drawPaint);
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (eleveSelectionne == null || !isSignatureActive())
		{
			return false;
		}

		float touchX = event.getX();
		float touchY = event.getY();  
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
		    drawPath.moveTo(touchX, touchY);
		    drawPath.lineTo(touchX, touchY);
		    break;
		case MotionEvent.ACTION_MOVE:
		    drawPath.lineTo(touchX, touchY);
			setSignatureActive(true);
			setZoneSignatureVide(false);
		    break;
		case MotionEvent.ACTION_UP:
		    drawCanvas.drawPath(drawPath, drawPaint);
		    drawPath.reset();
		    break;
		default:
		    return false;
		}
		invalidate();
		return true;
	}
	
	public void effacerZoneSignature(){
		setSignatureActive(true);
		setZoneSignatureVide(true);
		setupDrawing();
		if(canvasBitmap.isMutable())
		{
			drawCanvas = new Canvas(canvasBitmap);
		}
	    drawCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
	    invalidate();
	}
	
	public Bitmap enregistrerSignature()
	{
		// Enregistrement de la signature
        if(canvasBitmap == null)
        {
        	canvasBitmap =  Bitmap.createBitmap (this.getWidth(), this.getHeight(), Bitmap.Config.ARGB_8888);
        }   
        return canvasBitmap;
	}

	public boolean isSignatureActive() {
		return signatureActive;
	}
	
	public void chargerSignature (Bitmap signatureBitmap)
	{

		setupDrawing();
		canvasBitmap = Bitmap.createBitmap(signatureBitmap);
		drawCanvas.drawBitmap(canvasBitmap, 0, 0, null);
		this.setEnabled(false);
		invalidate();
	}
	


}
